package cn.dengta.webapp.ai.entity;

import java.util.Date;

import cn.dengta.context.model.WesKit;
import cn.dengta.context.web.RequestContextImpl;
import cn.dengta.webapp.ai.model.AiMediaFinishMode;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Cmp;
import me.codeplayer.util.EasyDate;

/**
 * ai语音通话记录
 *
 * @author zxy
 * @since 2024-04-23
 */
@Setter
@Getter
public class AiMediaLog extends TimeBasedEntity {

	private static final long serialVersionUID = 1L;

	/** 用户id */
	Long userId;
	/** Ai标识id */
	Long aiId;
	/** 通话总时长（单位：秒） */
	Integer duration;
	/** 免费时长 */
	Integer freeDuration;
	/** 通话费用 */
	Integer cost;
	/** 单位价格 */
	Integer unitPrice;
	/** 通话开始时间 */
	Date beginTime;
	/** 通话结束时间 */
	Date endTime;
	/** 状态：-1=拒绝；0=初始化；1=通话中；2=通话结束 */
	Integer status;
	/** 通话结束的方式 */
	AiMediaFinishMode finishMode;
	/** 结束时间 */
	Date finishTime;
	/** 速配ID标识 */
	Long speedId;
	/** 分包标识 */
	WesKit wesKit;

	public AiMediaLog init(WesKit wesKit, Long userId, Long aiId, boolean isSpeed) {
		final Date now = RequestContextImpl.get().now();
		this.userId = userId;
		this.aiId = aiId;
		this.status = isSpeed ? 0 : 1;
		initTime(now);
		// 舍弃小数位
		this.beginTime = this.endTime = new Date(System.currentTimeMillis() / 1000 * 1000 + 3000);
		return this;
	}

	public boolean hasPending() {
		return Cmp.eq(this.status, 0);
	}

	public boolean hasCall() {
		return Cmp.eq(this.status, 1);
	}

	public boolean hasFinished() {
		return Cmp.eq(this.status, 2);
	}

	/**
	 * 是否完成
	 */
	public boolean hasComplete() {
		return Cmp.eq(this.status, 2) || Cmp.eq(this.status, -1);
	}

	public static final String USER_ID = "user_id";
	public static final String AI_ID = "ai_id";
	public static final String DURATION = "duration";
	public static final String COST = "cost";
	public static final String BEGIN_TIME = "begin_time";
	public static final String END_TIME = "end_time";
	public static final String STATUS = "status";
	public static final String SPEED_ID = "speed_id";

}
